<template>
    <div class="workable-workableTask-container">
        <el-dialog title="修改计划任务" v-model="isShowDialog" >
            <el-form ref="editWorkableFormRef" :model="formData" :rules="formRules" size="small" label-width="80px">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="任务名称" prop="taskName">
                            <el-input v-model="formData.taskName" placeholder="请输入任务名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="任务排序" prop="orderNum">
							<el-input-number v-model="formData.orderNum" :min="0" :max="999" clearable class="w100"></el-input-number>
						</el-form-item>
					</el-col>
                    <el-col class="mb20">
                        <el-form-item label="备注" prop="remark">
                            <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onCancel" size="small">取 消</el-button>
                    <el-button type="primary" @click="onSubmit" size="small">保 存</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { editWorkableTask } from '/@/api/workable/task/index';
export default {
    name: 'workable/task/edit',
    setup(props, context) {
        const editWorkableFormRef = ref();
        const state = reactive({
            isShowDialog: false,
            formData: {},
            formRules : {
				taskName: { required: true, message: '任务名称不能为空', trigger: 'blur' },
				orderNum: { required: true, message: '任务排序不能为空', trigger: 'blur' }
            }
        });

        // 打开弹窗
        const openDialog = (row?: object) => {
            state.isShowDialog = true;
            if(row){
                state.formData = Object.assign({}, row);
            }
        };
        // 关闭弹窗
        const closeDialog = (row?: object) => {
            state.isShowDialog = false;
            for(var key in state.formData) {
                state.formData[key] = undefined;
            }
            editWorkableFormRef.value.resetFields();
        };
        // 取消
        const onCancel = () => {
            closeDialog();
        };
        // 新增
        const onSubmit = () => {
            editWorkableFormRef.value.validate(valid => {
                if(valid){
                    let params = {
                        id: state.formData.id,
                        taskName: state.formData.taskName,
						orderNum: state.formData.orderNum,
                        remark: state.formData.remark,
                    }
                    editWorkableTask(params).then((res) => {
                        if(res.code === 0){
                            ElMessage.success('保存成功');
                            context.emit('submit');
                            closeDialog();
                        }
                    });
                }
            });
        };
        return {
            editWorkableFormRef,
            openDialog,
            closeDialog,
            onCancel,
            onSubmit,
            ...toRefs(state),
        };
    },
};
</script>